package cn.itcast.bos.web.action;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.take_delivery.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.web.action.common.BaseAction;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("propotype")
public class promotionAction extends BaseAction<Propagation> {
	private Integer page;
	private Integer rows;

	public void setPage(Integer page) {
		this.page = page;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	@Action(value = "promotion_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		
		PageBean<Promotion> pageBean = WebClient
				.create("http://localhost:8888/bos_management/service/promotionService/findpagedata?page=" + page
						+ "&rows=" + rows)
				.accept(MediaType.APPLICATION_JSON).get(PageBean.class);
		//转换成我们想要的的对象
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("totalCount", pageBean.getTotalElements());
		result.put("pageData", pageBean.getContent());
		//转成我们想要的json格式
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}
}
